//
//  LogisticsHeader.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/6/10.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit

class ZyaLogisticsHeader: UIView, UITextFieldDelegate {
    
    var deleteImgBlock: ((Bool, VideoImg, Int) -> Void)?
    var addImgBlock: (([VideoImg]) -> Void)?
    var textSelectClosure: ((Int) -> ())?
    var textClosure: ((Int, String) -> ())?
    var textEndEditClosure: ((Int, String) -> ())?

    var hetongClosure: (() -> ())?
    var shouhuorenClosure: (() -> ())?
    var fahuorenClosure: (() -> ())?
    
    var segmentClosure: ((Int) -> ())?
    
    let leftWidth = 95
    
    private lazy var left1:  UILabel = {
        let v :UILabel = UILabel()
        v.text = "合同："
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    
    
    
    lazy var leftGys: UILabel = {
        let v :UILabel = UILabel()
        v.text = "供应商名称："
        v.textColor = .lightGray
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    private lazy var rightGys: ZyaCardField = {
        let v = ZyaCardField()
        v.tag = 2
        return v
    }()
    
    let leftPeriod = UILabel().easy("合同账期: ", nil, .systemFont(ofSize: 15))
    
    let rightPeriod = UILabel().easy(nil, .systemFont(ofSize: 15))
    
    let leftFile = UILabel().easy("上传发票", nil, .systemFont(ofSize: 15))
    
    var imageUtil: KSImageSelectUtil?
    
    var imageArray = [VideoImg]() {
        didSet {
            self.imageUtil?.newestShow(arr: imageArray)
        }
    }
    
    lazy var leftDate: UILabel = {
        let v :UILabel = UILabel()
        v.text = "发货日期："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftFaHuo: UILabel = {
        let v :UILabel = UILabel()
        v.text = "发货人："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftFhPhone: UILabel = {
        let v :UILabel = UILabel()
        v.text = "发货人电话："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftFhdq: UILabel = {
        let v :UILabel = UILabel()
        v.text = "发货人地区："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftShowHuo: UILabel = {
        let v :UILabel = UILabel()
        v.text = "收货人："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftShPhone: UILabel = {
        let v :UILabel = UILabel()
        v.text = "收货人电话："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftShdq: UILabel = {
        let v :UILabel = UILabel()
        v.text = "收货人地区："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var leftSeg: UILabel = {
        let v :UILabel = UILabel()
        v.text = "送货类型："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    
    lazy var leftLogisticsCompany: UILabel = {
        let v :UILabel = UILabel()
        v.text = "物流公司："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var rightLogisticsCompany: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请选择物流公司"
        v.tag = 12
        return v
    }()
    lazy var bottomBannar:UIView = {
        let v = UIView()
        return v
    }()
    
    lazy var leftLogisticsNo: UILabel = {
        let v :UILabel = UILabel()
        v.text = "物流单号："
        v.font = UIFont.systemFont(ofSize: 15)
        return v
    }()
    lazy var rightLogisticsNo: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请输入物流单号"
        v.tag = 13
        return v
    }()
    
    private(set) lazy var leftQsDate: UILabel = {
        return UILabel().then {
            $0.text = "签收日期："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    
    private(set) lazy var leftYfMoney: UILabel = {
        return UILabel().then {
            $0.text = "应付金额："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    private(set) lazy var rightYfMoney: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = ""
            $0.isUserInteractionEnabled = false
            $0.tag = 15
        }
    }()
    
    private(set) lazy var leftPayedMoeny: UILabel = {
        return UILabel().then {
            $0.text = "已付金额："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    private(set) lazy var rightPayedMoeny: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = ""
            $0.isUserInteractionEnabled = false
            $0.tag = 16
        }
    }()
    private lazy var rightHetong:  ZyaCardField = {
        let v = ZyaCardField()
        v.tag = 1
        v.placeholder = "请选择合同"
        return v
    }()
    lazy var hetong:BigRangeButton = {
        let v = BigRangeButton()
        v.setImage(UIImage(named:"hetong"), for: .normal)
        return v
    }()
    
    private lazy var right3: ZyaCardField = {
        let v = ZyaCardField()
        v.tag = 3
        v.placeholder = "请选择发货日期"
        return v
    }()
    lazy var rightFaHuo: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请输入发货人"
        v.tag = 4
        return v
    }()
    lazy var rightFhPhone: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请输入电话"
        v.keyboardType = .numberPad
        v.tag = 5
        return v
    }()
    lazy var rightFhdq: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请选择发货地区"
        v.tag = 6
        return v
    }()
    lazy var rightFhdz: UITextView_P = {
        let v = UITextView_P()
        v.placeHolder = "请输入详细地址"
        v.plaAlignment = .left
        v.textColor = .darkGray
        v.textAlignment = .left
        v.backgroundColor = UIColor(kshex:"#f9f9f9")
        v.font = UIFont.systemFont(ofSize: 15)
        v.tag = 7
        return v
    }()
    lazy var rightShowHuo: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请输入收货人"
        v.tag = 8
        return v
    }()
    lazy var shouhuoren:BigRangeButton = {
        let v = BigRangeButton()
        v.setImage(UIImage(named:"people"), for: .normal)
        return v
    }()
    lazy var fahuoren:BigRangeButton = {
        let v = BigRangeButton()
        v.setImage(UIImage(named:"people"), for: .normal)
        return v
    }()
    lazy var rightShPhone: ZyaCardField = {
        let v = ZyaCardField()
        v.placeholder = "请输入电话"
        v.keyboardType = .numberPad
        v.tag = 9
        return v
    }()
    lazy var rightShdq: ZyaCardField = {
        let v = ZyaCardField()
        v.tag = 10
        v.placeholder = "请选择收货地区"
        return v
    }()
    lazy var rightShdz: UITextView_P = {
        let v = UITextView_P()
        v.textColor = .darkGray
        v.textAlignment = .left
        v.backgroundColor = UIColor(kshex:"#f9f9f9")
        v.font = UIFont.systemFont(ofSize: 15)
        v.placeHolder = "请输入详细地址"
        v.plaAlignment = .left
        v.tag = 11
        return v
    }()
    lazy var rightSeg:UISegmentedControl = {
        let seg = UISegmentedControl(items: ["寄送","自送"])
        seg.selectedSegmentIndex = 0
        seg.tintColor = Theme.primaryColor()
        seg.addTarget(self, action: #selector(segmentDidchange(_:)), for: .valueChanged)
        return seg
    }()
    
    
    
    
    
    private(set) lazy var rightQsDate: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = ""
            $0.isUserInteractionEnabled = false
            $0.tag = 14
        }
    }()
    
    
    var excuteType: OperateType! {
        didSet {
            if excuteType != .add {
                hetong.isHidden = true
            } else {
                hetong.isHidden = false
            }
        }
    }
    
    lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 5
        return v
    }()
    
    @objc func segmentDidchange(_ sender: UISegmentedControl){
        if let clo = self.segmentClosure {
            clo(sender.selectedSegmentIndex)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(bg)
        bg.addSubview(left1)
        bg.addSubview(leftGys)
        bg.addSubview(leftPeriod)
        bg.addSubview(rightPeriod)
        bg.addSubview(leftDate)
        bg.addSubview(leftFaHuo)
        bg.addSubview(fahuoren)
        bg.addSubview(leftFhPhone)
        bg.addSubview(leftFhdq)
        bg.addSubview(leftShowHuo)
        bg.addSubview(shouhuoren)
        bg.addSubview(leftShPhone)
        bg.addSubview(leftShdq)
        bg.addSubview(leftSeg)
        bg.addSubview(leftLogisticsCompany)
        bg.addSubview(leftLogisticsNo)
        bg.addSubview(leftQsDate)
        bg.addSubview(leftYfMoney)
        bg.addSubview(leftPayedMoeny)
        
        bg.addSubview(rightQsDate)
        bg.addSubview(rightYfMoney)
        bg.addSubview(rightPayedMoeny)
        
        bg.addSubview(rightHetong)
        rightHetong.delegate = self
        bg.addSubview(hetong)
        bg.addSubview(bottomBannar)
        
        hetong.addTouchUpInSideAction { _ in
            if let clo = self.hetongClosure {
                clo()
            }
        }
        shouhuoren.addTouchUpInSideAction { _ in
            if let clo = self.shouhuorenClosure {
                clo()
            }
        }
        fahuoren.addTouchUpInSideAction { _ in
            if let clo = self.fahuorenClosure {
                clo()
            }
        }
        bg.addSubview(rightGys)
        rightGys.delegate = self
        bg.addSubview(right3)
        right3.delegate = self
        bg.addSubview(rightFaHuo)
        rightFaHuo.addTextChangeClosure { textx in
            if let clo = self.textClosure {
                clo(4,textx.text.unwrap)
            }
            
        }
        bg.addSubview(rightFhPhone)
        rightFhPhone.addTextChangeClosure { textx in
            if let clo = self.textClosure {
                clo(5,textx.text.unwrap)
            }
            
        }
        bg.addSubview(rightFhdq)
        rightFhdq.delegate = self
        bg.addSubview(rightFhdz)
        rightFhdz.addTextEndEditClosure { textx in
            if let clo = self.textEndEditClosure {
                clo(7,textx.text.unwrap)
            }
            
        }
        bg.addSubview(rightShowHuo)
        rightShowHuo.addTextChangeClosure { textx in
            if let clo = self.textClosure {
                clo(8,textx.text.unwrap)
            }
        }
        bg.addSubview(rightShPhone)
        rightShPhone.addTextChangeClosure { textx in
            if let clo = self.textClosure {
                clo(9,textx.text.unwrap)
            }
            
        }
        bg.addSubview(rightShdq)
        rightShdq.delegate = self
        bg.addSubview(rightShdz)
        rightShdz.addTextEndEditClosure { textx in
            if let clo = self.textEndEditClosure {
                clo(11,textx.text.unwrap)
            }
            
        }
        bg.addSubview(rightSeg)
        bg.addSubview(rightLogisticsCompany)
        rightLogisticsCompany.delegate = self
        bg.addSubview(rightLogisticsNo)
        rightLogisticsNo.addTextChangeClosure { textx in
            if let clo = self.textClosure {
                clo(13,textx.text.unwrap)
            }
            
        }
        
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeConstraints() {
        bg.snp.makeConstraints { (make) in
            make.left.top.equalTo(self).offset(6)
            make.bottom.right.equalTo(self).offset(-6)
        }
        
        left1.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightHetong)
            make.width.equalTo(leftWidth)
        }
        rightHetong.snp.makeConstraints { (make) in
            make.top.equalTo(bg).offset(6)
            make.right.equalTo(bg).offset(-30)
            
            make.height.equalTo(30)
            make.left.equalTo(left1.snp.right).offset(5)
        }
        hetong.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-3)
            make.centerY.equalTo(left1)
            make.height.equalTo(28)
            make.width.equalTo(28)
        }
        
        
        leftGys.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            
            make.centerY.equalTo(rightGys)
            make.width.equalTo(leftWidth)
        }
        rightGys.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightHetong.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftGys.snp.right).offset(5)
        }
        
        leftPeriod.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(10)
            make.top.equalTo(rightGys.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        
        rightPeriod.snp.makeConstraints { make in
            make.left.equalTo(leftPeriod.snp.right).offset(2)
            make.height.centerY.equalTo(leftPeriod)
            make.right.equalTo(bg).offset(-10)
        }
        
        leftDate.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(right3)
            make.width.equalTo(leftWidth)
        }
        right3.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightPeriod.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftDate.snp.right).offset(5)
        }
        
        
        
        leftFaHuo.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            
            make.centerY.equalTo(rightFaHuo)
            make.width.equalTo(leftWidth)
        }
        rightFaHuo.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-30)
            make.top.equalTo(right3.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftFaHuo.snp.right).offset(5)
        }
        fahuoren.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-3)
            make.centerY.equalTo(leftFaHuo)
            make.height.equalTo(28)
            make.width.equalTo(28)
        }
        
        
        leftFhPhone.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightFhPhone)
            make.width.equalTo(leftWidth)
        }
        rightFhPhone.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightFaHuo.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftFhPhone.snp.right).offset(5)
        }
        
        
        
        leftFhdq.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightFhdq)
            make.width.equalTo(leftWidth)
        }
        
        rightFhdq.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightFhPhone.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftFhdq.snp.right).offset(5)
        }
        rightFhdz.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightFhdq.snp.bottom).offset(2)
            make.height.equalTo(60)
            make.left.equalTo(leftFhdq.snp.right).offset(5)
        }
        
 
        leftShowHuo.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightShowHuo)
            make.width.equalTo(leftWidth)
        }
        
        rightShowHuo.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-30)
            make.top.equalTo(rightFhdz.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftShowHuo.snp.right).offset(5)
        }
        shouhuoren.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-3)
            make.centerY.equalTo(leftShowHuo)
            make.height.equalTo(28)
            make.width.equalTo(28)
        }
        
        leftShPhone.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightShPhone)
            make.width.equalTo(leftWidth)
        }
        rightShPhone.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightShowHuo.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftShPhone.snp.right).offset(5)
        }
        
        
        
        
        leftShdq.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightShdq)
            make.width.equalTo(leftWidth)
        }
        rightShdq.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightShPhone.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftShdq.snp.right).offset(5)
        }
        rightShdz.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightShdq.snp.bottom).offset(2)
            make.height.equalTo(60)
            make.left.equalTo(leftShdq.snp.right).offset(5)
        }
         
        
        leftSeg.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightSeg)
            make.width.equalTo(leftWidth)
        }
        rightSeg.snp.makeConstraints { (make) in
            make.width.equalTo(200)
            make.top.equalTo(rightShdz.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftShdq.snp.right).offset(5)
        }
        
        
        
        leftLogisticsCompany.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            
            make.centerY.equalTo(rightLogisticsCompany)
            make.width.equalTo(leftWidth)
        }
        rightLogisticsCompany.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightSeg.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftLogisticsCompany.snp.right).offset(5)
        }
        
        
        leftLogisticsNo.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightLogisticsNo)
            make.width.equalTo(leftWidth)
        }
        rightLogisticsNo.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightLogisticsCompany.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftLogisticsNo.snp.right).offset(5)
        }
        
        leftQsDate.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightQsDate)
            make.width.equalTo(leftWidth)
        }
        rightQsDate.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightLogisticsNo.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftQsDate.snp.right).offset(5)
        }
        
        
        
        leftYfMoney.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightYfMoney)
            make.width.equalTo(leftWidth)
        }
        rightYfMoney.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightQsDate.snp.bottom).offset(2)
            
            make.height.equalTo(30)
            make.left.equalTo(leftYfMoney.snp.right).offset(5)
        }
        
        
        leftPayedMoeny.snp.makeConstraints { (make) in
            make.left.equalTo(bg).offset(10)
            make.centerY.equalTo(rightPayedMoeny)
            make.width.equalTo(leftWidth)
        }
        rightPayedMoeny.snp.makeConstraints { (make) in
            make.right.equalTo(bg).offset(-10)
            make.top.equalTo(rightYfMoney.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.left.equalTo(leftPayedMoeny.snp.right).offset(5)
        }
        
        bg.addSubview(leftFile)
        leftFile.snp.makeConstraints { make in
            make.top.equalTo(rightPayedMoeny.snp.bottom).offset(2)
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(leftWidth)
            make.height.equalTo(30)
        }
        
        imageUtil = KSImageSelectUtil(frame: CGRect(x: 0, y: 0, width: KS_SCREEN_WIDTH - 2*6 , height: imgHeight), httpHeader: "", maxImgCount: 5, numberOfRow: 4, picType: [.pdfFile, .takePicture, .albumPicture], viewType: .editable)
        imageUtil?.collectionView?.isScrollEnabled = false
        imageUtil!.ksDelegate = self
        bg.addSubview(imageUtil!)
        imageUtil!.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(imageUtil!.ks_height)
            make.top.equalTo(leftFile.snp.bottom).offset(2)
        }
        
        bottomBannar.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(imageUtil!.snp.bottom)
            make.height.equalTo(5)
         }
    }
    
    var one = ZyaLogisticsModel(JSON:["":""])! {
        didSet {
            
            rightSeg.isUserInteractionEnabled = (excuteType == .view ? false : true)
            
            rightHetong.text = one.zType.unwrap + one.zContractNo.unwrap
            rightGys.text = one.zSupplierNm
            rightPeriod.text = (one.zAccPeriod != nil) ? " \(one.zAccPeriod!)天" : " -"
            right3.text = one.zConsignerDate
            rightFaHuo.text = one.zConsignerNm
            rightFhPhone.text = one.zConsignerTel
            rightFhdq.text = one.zProvinceFh.unwrap + one.zCityFh.unwrap + one.zDistrictFh.unwrap
            rightFhdz.text = one.zConsignerAddress
            
            // 手动计算文本高度
            var fhdzH = one.zConsignerAddress.unwrap.heightWithStringAttributes(attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15)], fixedWidth: KSGlobal.screenW - CGFloat(leftWidth) - 40) + 20
            if fhdzH < 50 {
                fhdzH = 50
            }
            rightFhdz.snp.remakeConstraints { (make) in
                make.right.equalTo(bg).offset(-10)
                make.top.equalTo(rightFhdq.snp.bottom).offset(2)
                make.height.equalTo(fhdzH)
                make.left.equalTo(leftFhdq.snp.right).offset(5)
            }
            
            
            rightShowHuo.text = one.zConsigneeNm
            rightShPhone.text = one.zConsigneeTel
            rightShdq.text = one.zProvinceSh.unwrap + one.zCitySh.unwrap + one.zDistrictSh.unwrap
            rightShdz.text = one.zConsigneeAddress
            
            // 手动计算文本高度
            var shdzH = one.zConsigneeAddress.unwrap.heightWithStringAttributes(attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15)], fixedWidth: KSGlobal.screenW - CGFloat(leftWidth) - 20) + 20
            if shdzH < 50 {
                shdzH = 50
            }
            rightShdz.snp.remakeConstraints { (make) in
                make.right.equalTo(bg).offset(-10)
                make.top.equalTo(rightShdq.snp.bottom).offset(2)
                make.height.equalTo(shdzH)
                make.left.equalTo(leftShdq.snp.right).offset(5)
            }
            
            
            rightLogisticsCompany.text = one.zLogisticsNm
            rightLogisticsNo.text = one.zLogisticsNo
            rightSeg.selectedSegmentIndex = intTran(one.zDeliveryType)
            
            rightQsDate.text = one.zSignDate.unwrap
            rightYfMoney.text = one.zPayAmount.unwrap
            rightPayedMoeny.text = one.zPaydAmount.unwrap
            
            if one.zDeliveryType == "0" { // 寄送
                leftLogisticsCompany.isHidden = false
                leftLogisticsNo.isHidden = false
                rightLogisticsCompany.isHidden = false
                rightLogisticsNo.isHidden = false
            } else {  // 自送
                leftLogisticsCompany.isHidden = true
                leftLogisticsNo.isHidden = true
                rightLogisticsCompany.isHidden = true
                rightLogisticsNo.isHidden = true
            }
            
            if one.zStatus == "3" { // 已签收
                leftQsDate.isHidden = false
                rightQsDate.isHidden = false
                rightQsDate.snp.remakeConstraints { (make) in
                    make.right.equalTo(bg).offset(-10)
                    if rightLogisticsNo.isHidden {
                        make.top.equalTo(rightSeg.snp.bottom).offset(2)
                    } else {
                        make.top.equalTo(rightLogisticsNo.snp.bottom).offset(2)
                    }
                    make.height.equalTo(30)
                    make.left.equalTo(leftQsDate.snp.right).offset(5)
                }
                
                leftYfMoney.isHidden = false
                leftPayedMoeny.isHidden = false
                
                rightYfMoney.isHidden = false
                rightPayedMoeny.isHidden = false
                
            } else if one.zStatus == "2" {
                leftQsDate.isHidden = false
                rightQsDate.isHidden = false
                
                leftYfMoney.isHidden = false
                rightYfMoney.isHidden = false
                
            } else if one.zStatus == "1" {
                leftQsDate.isHidden = false
                rightQsDate.isHidden = false
                
            } else {
                leftQsDate.isHidden = true
                rightQsDate.isHidden = true
                
                leftYfMoney.isHidden = true
                rightYfMoney.isHidden = true
                
                leftPayedMoeny.isHidden = true
                rightPayedMoeny.isHidden = true
            }
            
            leftFile.snp.remakeConstraints { make in
                
                if !rightPayedMoeny.isHidden {
                    make.top.equalTo(rightPayedMoeny.snp.bottom).offset(2)
                    
                } else if !rightYfMoney.isHidden {
                    make.top.equalTo(rightYfMoney.snp.bottom).offset(2)
                    
                } else if !leftQsDate.isHidden {
                    make.top.equalTo(leftQsDate.snp.bottom).offset(2)
                    
                } else if !rightLogisticsNo.isHidden {
                    make.top.equalTo(rightLogisticsNo.snp.bottom).offset(2)
                    
                } else {
                    make.top.equalTo(rightSeg.snp.bottom).offset(2)
                }
                                
//                if !rightPayedMoeny.isHidden {
//                    make.top.equalTo(rightPayedMoeny.snp.bottom).offset(2)
//                } else if !rightLogisticsNo.isHidden {
//                    make.top.equalTo(rightLogisticsNo.snp.bottom).offset(2)
//                } else {
//                    make.top.equalTo(rightSeg.snp.bottom).offset(2)
//                }
                
                make.left.equalToSuperview().offset(10)
                make.width.equalTo(leftWidth)
                make.height.equalTo(30)
            }
            
            imageUtil!.showAllFiles(files: [VideoImg]())
            
            
            
            let size = self.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
            let rect = CGRect(x: 0, y: 0, width: 0, height: size.height)
            self.frame = rect
        }
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if let clo = textSelectClosure {
            clo(textField.tag)
        }
        return false
    }
    
}




extension ZyaLogisticsHeader: KSImageUtilDelegate {
    func addImages(_ util:KSImageSelectUtil, imgvideos: [VideoImg]) {
        
        var tts = imgvideos
        // 把名称  data 处理好
        for i in 0..<tts.count {
            let fdata = resetImgSize(sourceImage: tts[i].image!, maxImageLenght: filemaxImageLenght, maxSizeKB: filemaxSizeKB)
            tts[i].data = fdata
            let fname = getAttachName(i: i)
            
            if tts[i].type == .image {
                tts[i].name = "\(fname).jpg"
            } else if tts[i].type == .video {
                tts[i].name = "\(fname).mp4"
            } else {
                tts[i].name = "\(fname).pdf"
            }
        }
        
        self.imageArray.append(contentsOf: tts)
        self.addImgBlock?(tts)
    }
    
    func addFiles(_ util: KSImageSelectUtil, files: [VideoImg]) {
        
        self.imageArray.append(contentsOf: files)
        self.addImgBlock?(files)
    }
    
    func deleteImage(_ util: KSImageSelectUtil, index: Int) {
        let one = self.imageArray[index]
        if one.idkey.unwrap.isEmpty {
            self.imageArray.remove(at: index)
            self.imageUtil!.deleteLocalImg(tag: index)
            deleteImgBlock?(false, one, index)
        } else {
            deleteImgBlock?(true, one, index)
        }
 
     }
}
